package com.itgone.view.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.andview.refreshview.utils.LogUtils;
import com.itgone.R;
import com.itgone.bean.Product;
import com.itgone.common.Const;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

public class HomeRVAdapter extends ClickableRcvAdapter<HomeRVAdapter.MyViewHolder> {
    private List<Product> productList;
    private Context mContext;

    public HomeRVAdapter(Context context) {
        mContext = context;
    }

    public void setDataList(List<Product> productList) {
        this.productList = productList;
    }

    public List<Product> getProductList() {
        return productList;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        MyViewHolder holder = new MyViewHolder(LayoutInflater.from(
                mContext).inflate(R.layout.item_home_rv, parent,
                false));
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
        bindListener(holder);
        Product product = productList.get(position);
        holder.tvName.setText(product.getName());
        holder.tvPrice.setText("￥" + product.getPrice().toString());
        String url = Const.URL_IMAGE_BASE + product.getMainImage();
        LogUtils.e("url = " + url);
        Picasso.with(mContext).load(url)
                .resize(400, 400)
                .centerCrop()
                .into(holder.ivIcon);
    }

    @Override
    public int getItemCount() {
        return productList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private TextView tvName;
        private TextView tvPrice;
        private ImageView ivIcon;

        public MyViewHolder(View view) {
            super(view);
            tvName = view.findViewById(R.id.tv_product_name);
            tvPrice = view.findViewById(R.id.tv_price);
            ivIcon = view.findViewById(R.id.iv_product_icon);
        }
    }
}
